package com.bolingcavalry.crm.product.controller;


import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.product.service.ProductService;
import com.dto.ProductDTO;
import com.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/products")
@Api(tags = "产品管理接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/list")
    @ApiOperation("分页查询产品列表，支持按名称、型号、规格筛选")
    public ResponseResult<PageResult<ProductVO>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String version,
            @RequestParam(required = false) String specification,
            @RequestParam(required = false) String problemCategory
    ){
        PageResult<ProductVO> pageResult = productService.getProductsByConditions(pageNum, pageSize, name, version, specification,problemCategory);
        return ResponseResult.success("查询成功", pageResult);
    }



    @GetMapping("get/{id}")
    @ApiOperation("根据ID获取产品详情")
    public ResponseResult<ProductVO> getProductById(@PathVariable Long id){
        return ResponseResult.success(productService.getProductById(id));
    }

    @PostMapping("/add")
    @ApiOperation("新增产品")
    public ResponseResult createProduct(@RequestBody ProductDTO productDTO){
        return productService.createProduct(productDTO);
    }

    @PostMapping("/update")
    @ApiOperation("更新产品")
    public ResponseResult updateProduct(@RequestBody ProductDTO productDTO){
        return ResponseResult.success(productService.updateProduct(productDTO));
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation("删除产品")
    public ResponseResult deleteProduct(@PathVariable Long id) {
        productService.deleteProduct(id);
        return ResponseResult.success("删除成功");
    }
}